package com.wgchao.authmanager.query;

import com.wgchao.authmanager.application.vo.HandleVo;
import com.wgchao.authmanager.application.vo.MenuVo;
import com.wgchao.authmanager.application.vo.ProjectVo;
import com.wgchao.authmanager.infrastructure.exception.ObjectDisabledException;
import com.wgchao.authmanager.infrastructure.exception.ObjectNotExistException;
import com.wgchao.authmanager.infrastructure.jpa.entity.HandleInfo;
import com.wgchao.authmanager.infrastructure.jpa.entity.MenuInfo;
import com.wgchao.authmanager.infrastructure.jpa.entity.ProjectInfo;
import com.wgchao.authmanager.infrastructure.jpa.repository.HandleInfoJpaRepository;
import com.wgchao.authmanager.infrastructure.jpa.repository.MenuInfoJpaRepository;
import com.wgchao.authmanager.infrastructure.jpa.repository.ProjectInfoJpaRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: ProjectInfoQuery
 * @author: wgchao
 * @createTime: 2020/5/9 10:48 AM
 * @Description: TODO
 */
@Service
public class ProjectInfoQuery {
    @Autowired
    private ProjectInfoJpaRepository projectInfoJpaRepository;
    @Autowired
    private MenuInfoJpaRepository menuInfoJpaRepository;
    @Autowired
    private HandleInfoJpaRepository handleInfoJpaRepository;

    /**
     * 查询项目列表
     * @param disabled null返回全部
     * @return
     */
    public List<ProjectVo> searchAllProjects(Boolean disabled){
        List<ProjectInfo> projectInfos = null;
        if(disabled == null){
            projectInfos = projectInfoJpaRepository.findAllByDelIsFalse();
        }else{
            projectInfos = projectInfoJpaRepository.findAllByDisabledAndDelIsFalse(disabled);
        }
        if(projectInfos == null){
            return new ArrayList<>();
        }
        List<ProjectVo> projectVos = new ArrayList<>();
        for(ProjectInfo projectInfo : projectInfos){
            ProjectVo projectVo = new ProjectVo();
            BeanUtils.copyProperties(projectInfo, projectVo);
            projectVos.add(projectVo);
        }
        return projectVos;
    }

    /**
     * 查询项目下的菜单目录
     * @param projectIdentify
     * @return
     */
    public List<MenuVo> searchMenusOfProject(String projectIdentify){
        ProjectInfo projectInfo = projectInfoJpaRepository.findFirstByIdentifyAndDelIsFalse(projectIdentify);
        if(projectInfo == null){
            throw new ObjectNotExistException("项目资源不存在");
        }
        if(projectInfo.isDisabled()){
            throw new ObjectDisabledException("项目资源不可用");
        }
        List<MenuInfo> menuInfos = menuInfoJpaRepository.findAllByProjectIdentify(projectIdentify);
        if(menuInfos == null){
            return new ArrayList<>();
        }
        List<MenuVo> menuVos = menuInfos.stream().map(menuInfo -> {
            MenuVo menuVo = new MenuVo();
            menuVo.setProjectIdentify(projectIdentify);
            menuVo.setProjectName(projectInfo.getName());
            menuVo.setMenuIdentify(menuInfo.getMenuIdentify());
            menuVo.setMenuName(menuInfo.getName());
            menuVo.setRemark(menuInfo.getRemark());
            return menuVo;
        }).collect(Collectors.toList());

        return menuVos;
    }

    /**
     * 查询操作资源目录
     * @return
     */
    public List<HandleVo> searchHandles(){
        List<HandleInfo> handleInfos = handleInfoJpaRepository.findAll();
        if(handleInfos == null){
            return new ArrayList<>();
        }
        List<HandleVo> handleVos = handleInfos.stream().map(handleInfo -> {
            HandleVo handleVo = new HandleVo();
            BeanUtils.copyProperties(handleInfo, handleVo);
            return handleVo;
        }).collect(Collectors.toList());

        return handleVos;
    }
}
